package com.xm.xmapp;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ZFBInterFace;
import com.xm.xmapp.request.bean.BaseBeanRequest;
import com.xm.xmapp.request.bean.Get_longrent_order_pay_signRequest;
import com.xm.xmapp.request.bean.Get_longrent_subscribe_pay_signRequset;
import com.xm.xmapp.request.bean.LongRentPayWalletRequest;
import com.xm.xmapp.response.bean.ResponseWXpaysign;
import com.xm.xmapp.response.bean.ResponseAddPayOrder;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.tools.PayUitl;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;
import com.xm.xmapp.wxapi.WXPayEntryActivity;

public class LongRentOrderPayActivity extends BaseActivity implements OnClickListener {
	private TextView pay;
	private RelativeLayout rel0, rel1, rel2;
	private int flag;// 0是钱包1是支付宝2是微信
	private ImageView img0, img1, img2;
	private TextView  money, orderMoney, tv_money;
	private String longRentMoney;
	private int payType;
	private String order_id;
	private int tenancy_type;
	private boolean isReRent;// 是否是续租，续租才有钱包支付
	private ResponseUserMoney moneyBean;// 钱包信息

	/**
	 * 支付成功后執行
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_LongRentOrderPay.DEFAULT:
			finish();
			break;

		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longrent_pay_order);
	}

	@Override
	public void initView() {
		pay = (TextView) findViewById(R.id.pay);
		rel0 = (RelativeLayout) findViewById(R.id.rel0);
		rel1 = (RelativeLayout) findViewById(R.id.rel1);
		rel2 = (RelativeLayout) findViewById(R.id.rel2);
		img0 = (ImageView) findViewById(R.id.img0);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		money = (TextView) findViewById(R.id.money);
		tv_money = (TextView) findViewById(R.id.tv_money);
		orderMoney = (TextView) findViewById(R.id.orderMoney);
	}

	@Override
	public void initData() {
		setMyTitle("订单支付");
		order_id = getIntent().getStringExtra("order_id");
		longRentMoney = getIntent().getStringExtra("longRentMoney");
		tenancy_type = getIntent().getIntExtra("tenancy_type", 1);
		isReRent = getIntent().getBooleanExtra("isReRent", false);
		money.setText(longRentMoney + "元");

		if (isReRent) {
			orderMoney.setText("车辆长时续用金额");
			rel0.setVisibility(View.VISIBLE);
			moneyBean = (ResponseUserMoney) getIntent().getSerializableExtra("moneyBean");
			tv_money.setText(StringTools.getString(moneyBean.user_money, "0.00") + "元");
			if (moneyBean.user_money >= Double.parseDouble(longRentMoney)) {
				payType = 5;
				flag = 0;
				setImg();
			} else {
				flag = 1;
				payType = 2;// 6=微信app支付 2=支付宝支付
				setImg();
			}

		} else {
			orderMoney.setText("车辆长时预约订金");
			rel0.setVisibility(View.GONE);
			flag = 1;
			payType = 2;// 6=微信app支付 2=支付宝支付
			setImg();
		}

	}

	@Override
	public void initEvent() {
		pay.setOnClickListener(this);
		rel0.setOnClickListener(this);
		rel1.setOnClickListener(this);
		rel2.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pay:
			if (isReRent) {
				if (flag != 0) {
					WXPayEntryActivity.currentStatus = WXPayEntryActivity.LONGRERENT;
					Get_longrent_order_pay_signRequest requestBean = new Get_longrent_order_pay_signRequest();
					requestBean.money = longRentMoney;
					requestBean.payType = payType;
					requestBean.order_id = order_id;
					requestBean.tenancy_type = tenancy_type;
					startToPay(requestBean);
				} else {
					LongRentPayWalletRequest requestBean = new LongRentPayWalletRequest();
					requestBean.money = longRentMoney;
					requestBean.payType = payType;
					requestBean.order_id = order_id;
					requestBean.tenancy_type = tenancy_type;
					MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

						@Override
						public void onResponse(String response) {
							promptDialog.dismiss();
							PayUitl.getInstence().afterPayMoney();
						}
					}, new DisssmissInterFace() {

						@Override
						public void putError(VolleyError error) {
							promptDialog.dismiss();
						}
					});
				}

			} else {
				WXPayEntryActivity.currentStatus = WXPayEntryActivity.LONGYUYUE;
				Get_longrent_subscribe_pay_signRequset requestBean = new Get_longrent_subscribe_pay_signRequset();
				requestBean.payType = payType;
				requestBean.subscribe_id = order_id;
				startToPay(requestBean);
			}

			break;
		case R.id.rel0:
			if (moneyBean.user_money >= Double.parseDouble(longRentMoney)) {
				payType = 5;
				flag = 0;
				setImg();
			} else {
				Tools.showMessage("钱包余额不足，请选择其他支付方式");
			}

			break;
		case R.id.rel1:
			payType = 2;
			flag = 1;
			setImg();
			break;

		case R.id.rel2:
			payType = 6;
			flag = 2;
			setImg();
			break;
		default:
			break;
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
	}

	private void startToPay(BaseBeanRequest requestBean) {
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				try {
					if (flag == 1) {
						ResponseAddPayOrder bean = JSON.parseObject(response, ResponseAddPayOrder.class);
						PayUitl.getInstence().zfb(LongRentOrderPayActivity.this, new ZFBInterFace() {

							@Override
							public void success() {
								PayUitl.getInstence().afterPayMoney();
							}
						}, bean.sign_string);
					} else {
						JSONObject obj = JSONObject.parseObject(response);
						ResponseWXpaysign base = JSON.parseObject(response, ResponseWXpaysign.class);
						base.package_str = obj.getString("package");
						PayUitl.getInstence().genWxPay(LongRentOrderPayActivity.this, base, promptDialog);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	private void setImg() {

		if (flag == 1) {
			img0.setImageResource(R.mipmap.icon_xuanze_dis);
			img1.setImageResource(R.mipmap.icon_xuanze_nor);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		} else if (flag == 2) {
			img0.setImageResource(R.mipmap.icon_xuanze_dis);
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_nor);
		} else {
			img0.setImageResource(R.mipmap.icon_xuanze_nor);
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		}
	}

}
